package com.mx.kn.cstf2.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import kn.cstf2.assign.Bean.ListaTablaBean;
import kn.cstf2.assign.Server.AssignServer;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.main.Server.MainServer;
import kn.cstf2.main.Shared.MainShared;
import kn.cstf2.transition.Bean.TransitionBean;

import com.mx.kn.cstf2.actions.padre.Transition;
import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.service.AssignService;
import com.mx.kn.cstf2.service.MainService;
import com.mx.kn.cstf2.service.TransitionService;
import com.mx.kn.cstf2.utils.TipoUsuarios;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author user1
 */
@Component("mainAction")
@Scope(value = "session")
public class MainAction extends ActionSupport {

	@Autowired
	AssignService assignService;
	@Autowired
	MainService mainService;
	@Autowired
	TransitionService transitionService;

	public static final String ERROR_SESSION = "errorSession";
	private TransitionBean transitionBean = new TransitionBean();
	private ArrayList<TransitionBean> transitionBeanArray;
	private ArrayList<ListaTablaBean> listaTablaBeans;
	private String knkey;
	private Integer knkey2;
	private String companyName;
	private ListaTablaBean knKeyFound;
	private UsuarioDto usuario = new UsuarioDto();
	private Integer idTransition;
	private Transition transition;
	
	private List<ListaTablaBean> copCompanyFound;
	private String copCompanyName;
	
	private boolean flujoVentas;
	
	
	public boolean isFlujoVentas() {
		return flujoVentas;
	}

	public void setFlujoVentas(boolean flujoVentas) {
		this.flujoVentas = flujoVentas;
	}

	public String getCopCompanyName() {
		return copCompanyName;
	}

	public void setCopCompanyName(String copCompanyName) {
		this.copCompanyName = copCompanyName;
	}

	public List<ListaTablaBean> getCopCompanyFound() {
		return copCompanyFound;
	}

	public void setCopCompanyFound(List<ListaTablaBean> copCompanyFound) {
		this.copCompanyFound = copCompanyFound;
	}

	public String getKnkey() {
		return knkey;
	}

	public void setKnkey(String knkey) {
		this.knkey = knkey;
	}

	public ArrayList<TransitionBean> getTransitionBeanArray() {
		return transitionBeanArray;
	}

	public void setTransitionBeanArray(
			ArrayList<TransitionBean> transitionBeanArray) {
		this.transitionBeanArray = transitionBeanArray;
	}

	public ArrayList<ListaTablaBean> getListaTablaBeans() {
		return assignService.obtenerListaTabla(getUsuario().getId());
	}

	public void setListaTablaBeans(ArrayList<ListaTablaBean> listaTablaBeans) {
		this.listaTablaBeans = listaTablaBeans;
	}

	public Integer getKnkey2() {
		return knkey2;
	}

	public void setKnkey2(Integer knkey2) {
		this.knkey2 = knkey2;
	}

	public MainAction() {
//
//		ActionContext contexto = ActionContext.getContext();
//		AssignServer assignServer = new AssignServer();
//		Map<String, Object> sesion = contexto.getSession();
//		this.setUsuario((UsuarioDto) sesion.get("usuarioSession"));
//
//		MainShared mainShared = new MainShared();
//		setTransitionBeanArray(mainShared
//				.obtenerKnkeysandClientNames(getUsuario()));
	}

	@Override
	public String execute() {

		try {
			if (transitionBean != null) {
				ActionContext contexto = ActionContext.getContext();
				Map<String, Object> sesion = contexto.getSession();
				setUsuario((UsuarioDto) sesion.get("usuarioSession"));
				cargarKnkeyOnSession();
			} else {

//				addFieldError("KnkeyIncorrecta", "No hay Knkeys asignadas.");
				return ERROR_SESSION;
			}
		} catch (CstF2Exception ex) {

//			addFieldError("KnkeyIncorrecta",
//					"Ocurrio un Error: " + ex.getMessage());
			return ERROR_SESSION;
		}
		return SUCCESS;
	}

	public void cargarKnkeyOnSession() {
		TransitionBean transitionBean = new TransitionBean();
		if (knkey2 != null) {
			transitionBean.setId(idTransition);
			transitionBean.setKnkey(knkey2);

			ActionContext contexto = ActionContext.getContext();
			Map<String, Object> sesion2 = contexto.getSession();
			sesion2.put("Knkey", transitionBean);
//			mainService.updatedFalse(knkey2);
		}
	}

	public String searchKnKey() {

		try {
			ListaTablaBean listaTable = mainService.getCliente(Integer
					.parseInt(knkey));
			if (listaTable.getKnkey() != null)
				this.setKnKeyFound(mainService.getCliente(Integer
						.parseInt(knkey)));
//			else
//				addFieldError("error", "Key no existe");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException nFE) {
//			addFieldError("error", "Key  no valida");
			Log.info("no es numero");

		}
		return SUCCESS;
	}
	
	public String searchCopKnKey() throws SQLException {
		
		this.setCopCompanyFound(mainService.getCopClient(getCopCompanyName(), getUsuario().getTipo()));

		return SUCCESS;
	}

	public String entradaHouseAccount() {

		// checar si transaction ya existe
		try {
			TransitionBean transitionBean = new TransitionBean();
			transitionBean.setKnkey(knkey2);
			ActionContext contexto = ActionContext.getContext();
			Map<String, Object> sesion = contexto.getSession();
			sesion.put("Knkey", transitionBean);
			
			switch (this.getUsuario().getTipo()) {
			case TipoUsuarios.USER_FS:
				transition = new TransitionFS();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FS);

				break;
				
			case TipoUsuarios.USER_FO:
				transition = new TransitionFO();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FO);
				
				break;

			case TipoUsuarios.USER_FA:
				transition = new TransitionFA();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FA);

				break;
			default:
				break;
			}
					
			if (!transitionService.checkIfExistTransaction(Integer.parseInt(knkey)))		
				transitionService.newTransition(knkey2, companyName,this.getUsuario());
			
			transition.setTransitionBeanM(transitionService.getTransitionBeanIdKnkey(transition.getTransitionBeanM()));
			if(!mainService.validaExistenciaCliente(getUsuario().getId(), transition.getTransitionBeanM().getId()))
				mainService.insertClient(getUsuario().getId(), transition.getTransitionBeanM().getId());

		} catch (NumberFormatException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return SUCCESS;
	}
	
	public String entradaSerchCop() {

		// checar si transaction ya existe
		try {
			TransitionBean transitionBean = new TransitionBean();
			transitionBean.setKnkey(knkey2);
			ActionContext contexto = ActionContext.getContext();
			Map<String, Object> sesion = contexto.getSession();
			sesion.put("Knkey", transitionBean);
			
			switch (this.getUsuario().getTipo()) {
			case TipoUsuarios.USER_FS:
				transition = new TransitionFS();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FS);

				break;
				
			case TipoUsuarios.USER_FO:
				transition = new TransitionFO();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FO);
				
				break;

			case TipoUsuarios.USER_FA:
				transition = new TransitionFA();
				transition.setTransitionBeanM((TransitionBean) sesion.get("Knkey"));
				transition.getTransitionBeanM().setTipoTransition(TipoUsuarios.USER_FA);

				break;
			default:
				break;
			}
					
			if (!transitionService.checkIfExistTransaction(knkey2))		
				transitionService.newTransition(knkey2, companyName,this.getUsuario());
			
			setFlujoVentas(transitionService.checkFlow(knkey2,this.getUsuario()));			
			sesion.put("flujo", flujoVentas);

		} catch (NumberFormatException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return SUCCESS;
	}

	public UsuarioDto getUsuario() {
		return usuario;
	}

	public void setUsuario(UsuarioDto usuario) {
		this.usuario = usuario;
	}

	public ListaTablaBean getKnKeyFound() {
		return knKeyFound;
	}

	public void setKnKeyFound(ListaTablaBean knKeyFound) {
		this.knKeyFound = knKeyFound;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public Integer getIdTransition() {
		return idTransition;
	}

	public void setIdTransition(Integer idTransition) {
		this.idTransition = idTransition;
	}
	
	

}
